﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data.Common;
using System.Data;
using medicine.DAL.DataSet1TableAdapters;
using medicine.General;

namespace medicine.BaseComponents.Groups
{
    public class UnitsCustomDataSource : IGroupDataSource
    {

        public DbDataReader GetDataSource()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("Value");

            DataRow dr = dt.NewRow();
            dr["Name"] = "Units";
            dr["Value"] = new UnitsClass("Units", "curUnits").Serialize();           
            dt.Rows.Add(dr);

            DataTable dtExchangeRates=new CurrencyExchangeTableAdapter().GetData();

            foreach (DataRow item in dtExchangeRates.Rows )
            {
                dr = dt.NewRow();
                dr["Name"] = item["Currency"].ToString();
                dr["Value"] = new UnitsClass(item["Currency"].ToString(), (decimal)item["ExchangeRate"]).Serialize();   
                dt.Rows.Add(dr); 
            }



            return dt.CreateDataReader();           
        }
    }

    public class UnitsClass
    {
        public string UnitName = "";
        public decimal ExchangeRate = 1;
        public string FieldName = "";
        public bool isCurrency = true;

        public UnitsClass()
        {

        }

        public UnitsClass(string UnitName, decimal ExchangeRate)
        {
            this.UnitName = UnitName;
            this.ExchangeRate = ExchangeRate;
            this.FieldName = "curValue_" + UnitName;
            this.isCurrency = true;
        }

        public UnitsClass(string UnitName, string  FieldName)
        {
            this.UnitName = UnitName;
            this.ExchangeRate = 1;
            this.FieldName = FieldName;
            this.isCurrency = false;
        }

        public string Serialize()
        {
            return SerializingUtility.SerializeObjectToXMLString<UnitsClass>(this);
        }

        public static UnitsClass DeSerialize(string XML)
        {
            return SerializingUtility.DeserializeObjectFromXMLString<UnitsClass>(XML);
        }

        public static List<UnitsClass> ValuesToUnits(List<string> Values)
        {
            List<UnitsClass> Result=new List<UnitsClass>();
            foreach (string item in Values )
	        {
		        Result.Add(UnitsClass.DeSerialize(item));
	        }

            return Result;
        }

    }
}
